﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Com.Framework;
using Com.Framework.Enc;
using Com.Framework.Cookie;
using Com.Library.DB.User;
using Com.Library.Cookie;

namespace Com.Web.coffinegurunaru.login
{
    public partial class loginproc : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
			string Redirect = Request.Form["redirect"].Parse(string.Empty);
			string UserID = Request.Form["UserID"].Parse(string.Empty);
			string UserPWD = Request.Form["UserPWD"].Parse(string.Empty);

			byte LoginErrorType = UserModel.UserLogin(UserID, UserPWD, Request.UserHostAddress);

			if (LoginErrorType == 1 || LoginErrorType == 3)
			{
				ClientScript.RegisterClientScriptBlock(this.GetType(), "loginerror", @"
<script language='javascript'>
alert('존재하지 않는 아이디입니다.');
history.go(-1);
</script>
");
				return;
			}
			else if (LoginErrorType == 2)
			{
				ClientScript.RegisterClientScriptBlock(this.GetType(), "loginerror", @"
<script language='javascript'>
alert('비밀번호가 틀렸습니다.');
history.go(-1);
</script>
");
				return;
			}

			if (LoginErrorType == 0)
			{
				try
				{
					UserEntity info = UserModel.UserInfo(UserID);
					CookieParam param = new CookieParam();
					param.Add("UserNo", info.UserNo.ToString());
					param.Add("UserID", UserID);
					param.Add("UserName", info.UserName);
					param.Add("GroupType", info.GroupType.ToString());
					param.Add("IsLocalUser", info.IsLocalUser);
					param.Add("UserLevel", info.UserLevel.ToString());
					WebCookie cookie = new WebCookie();
					cookie.Clear();
					cookie.SetEncCookie(param);


					if (Redirect == string.Empty)
					{
						Redirect = "/default.aspx";
					}
					ClientScript.RegisterClientScriptBlock(this.GetType(), "loginproc", @"
<script language='javascript'>
location.href=""" + Redirect + @"""
</script>
				");
				}
				catch
				{
					ClientScript.RegisterClientScriptBlock(this.GetType(), "loginerror", @"
<script language='javascript'>
alert('로그인중 오류가 발생하였습니다\n다시 시도해주세요.');
history.go(-1);
</script>
");
				}
			}
			else
			{
				ClientScript.RegisterClientScriptBlock(this.GetType(), "loginerror", @"
<script language='javascript'>
alert('로그인중 오류가 발생하였습니다\n다시 시도해주세요.');
history.go(-1);
</script>
");
				return;
			}
        }
    }
}
